package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SearchController {


    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("q")
    public String saarch(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        String viewName = null;
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_DEST:
                searchDest(qo, model);

                viewName = "searchDest";
                break;
            case SearchQueryObject.SEARCH_STRATEGY:
                searchStrategy(qo, model);

                viewName = "searchStrategy";
                break;
            case SearchQueryObject.SEARCH_TRAVEL:
                searchTravel(qo, model);

                viewName = "searchTravel";
                break;
            case SearchQueryObject.SEARCH_USER:
                searchUser(qo, model);
                viewName = "searchUser";
                break;
            default:
                serarchAll(qo,model);
                viewName = "searchAll";

        }
        return "index/" + viewName;
    }

    private void serarchAll(SearchQueryObject qo, Model model) {
        int total=0;
        Page<UserInfoTemplate> users = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class
                , qo, "nickname", "city");
        total+=users.getTotalElements();
//        标题，摘要
        Page<TravelTemplate> travels = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class
                , qo, "title", "summary");
        total+=travels.getTotalElements();
//        标题，副标题，摘要
        Page<StrategyTemplate>  strategys = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class
                , qo, "title", "subTitle", "summary");
        total+=strategys.getTotalElements();
        //dest
        Page<DestinationTemplate> dests = searchService.hightLightSearch(DestinationTemplate.INDEX_TYPE_NAME, DestinationTemplate.class, qo, "name");
        total+=dests.getTotalElements();

        SearchResult data = new SearchResult();
        data.setStrategys(strategys.getContent());
        data.setTravels(travels.getContent());
        data.setUsers(users.getContent());
        data.setDests(dests.getContent());
        data.setTotal(total);
        model.addAttribute("data", data);



    }

    private void searchUser(SearchQueryObject qo, Model model) {
//        昵称，城市名
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class
                , qo, "nickname", "city");
        model.addAttribute("page", page);
    }

    private void searchTravel(SearchQueryObject qo, Model model) {
//        标题，摘要
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class
                , qo, "title", "summary");
        model.addAttribute("page", page);
    }

    private void searchStrategy(SearchQueryObject qo, Model model) {
//        标题，副标题，摘要
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class
                , qo, "title", "subTitle", "summary");
        model.addAttribute("page", page);
    }


    private void searchDest(SearchQueryObject qo, Model model) {
        //dest
        DestinationTemplate dest = destinationSearchService.listByName(qo.getKeyword());
        model.addAttribute("dest", dest);
        //设置总数
        int total = 0;

        if (dest != null) {
            total = 1;
        }

        //data.  strategys travels.users
        List<StrategyTemplate> sts = strategySearchService.listByDestName(qo.getKeyword());
        total += sts.size();
        List<TravelTemplate> tts = travelSearchService.listByDestName(qo.getKeyword());
        total += tts.size();
        List<UserInfoTemplate> uts = userInfoSearchService.listByDestName(qo.getKeyword());
        total += uts.size();
        SearchResult data = new SearchResult();
        data.setStrategys(sts);
        data.setTravels(tts);
        data.setUsers(uts);
        data.setTotal(total);

        model.addAttribute("data", data);
    }


}
